package org.osite.util;

import org.osite.xml.XmlConstructor;
import org.osite.xml.XmlParam;

public class Series {
	
	private String fromString;
	private String toString;
	private String stepString;
	
	private int from;
	private int to;
	private int step;
	private String name;
	
	@XmlConstructor
	public Series(
			@XmlParam("name") String name, 
			@XmlParam("from") String from, 
			@XmlParam("to") String to, 
			@XmlParam(value = "step", preset = "1") String step) {
		this.name = Preconditions.checkNotNull(name);
		fromString = Preconditions.checkNotNull(from);
		toString = Preconditions.checkNotNull(to);
		stepString = Preconditions.checkNotNull(step);
	}
	
	public String getName() {
		return name;
	}
	
	private static int getVariable(Combination comb, String value) {
		final int VARIABLE_OFFSET = 1;
		return Integer.parseInt(value.startsWith("@") 
				? comb.get(value.substring(VARIABLE_OFFSET)) 
				: value);
	}
	
	public void reset(Combination comb) {
		from = getVariable(comb, fromString);
		to = getVariable(comb, toString);
		step = getVariable(comb, stepString);
	}
	
	public boolean hasNext() {
		return step > 0 ? from <= to : from >= to;
	}
	
	public String next() {
		String returnValue = Integer.toString(from);
		from += step;
		return returnValue;
	}
}
